﻿using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE80;
using EnvDTE;
using System.Windows.Forms;

using Microsoft.VisualStudio.CommandBars;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace TFSPlus.Commands
{
    public class FindFileInTfsExplorer : IAddinUICommand
    {
        #region IAddinUICommand Members

        public void UISetup(DTE2 applicationObject, AddIn addInInstance, Commands2 commands, object[] contextGUIDS)
        {
            Command findFileCommand = commands.AddNamedCommand2(addInInstance, "FindFileInTfsExplorer", "Find File in TFS Explorer", "Navigates to this file in the TFS Explorer window", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

            CommandBarControl openFolderInWinExplorerFolder = Connect.GetCommandBarControl("Folder", "Open Folder in Windows Explorer", null);
            int faceIdOpenFolderInWinExplorer = (openFolderInWinExplorerFolder != null && openFolderInWinExplorerFolder is CommandBarButton) ? ((CommandBarButton)openFolderInWinExplorerFolder).FaceId : 59;

            // Add a command to the Item shortcut menu.
            AddCommandToCommandBar(findFileCommand, "Item", "Find File in Source Control Explorer", faceIdOpenFolderInWinExplorer);

            // Add a command to the Folder shortcut menu.
            AddCommandToCommandBar(findFileCommand, "Folder", "Find Folder in Source Control Explorer", faceIdOpenFolderInWinExplorer);

            // Add a command to the Project shortcut menu.
            AddCommandToCommandBar(findFileCommand, "Project", "Find Folder in Source Control Explorer", faceIdOpenFolderInWinExplorer);

            // Add a command to the Solution shortcut menu.
            AddCommandToCommandBar(findFileCommand, "Solution", "Find Folder in Source Control Explorer", faceIdOpenFolderInWinExplorer);

            //CommandBarControl propertiesButtonItem = Connect.GetCommandBarControl(commandBarName, "Properties", null);
            //int propertiesStartIndexItem = (propertiesButtonItem == null ? -1 : propertiesButtonItem.Index + 1);

            CommandBar commandBar = Connect.GetCommandBar("Code Window", new string[] { "Source Control" });
            CommandBarButton shortcutCtrl = (CommandBarButton)findFileCommand.AddControl(commandBar, commandBar.Controls.Count);
            shortcutCtrl.FaceId = faceIdOpenFolderInWinExplorer;
            
        }

        private static void AddCommandToCommandBar(Command command, string commandBarName, string commandCaption, int faceId)
        {
            CommandBarControl propertiesButtonItem = Connect.GetCommandBarControl(commandBarName, "Properties", null);
            int propertiesStartIndexItem = (propertiesButtonItem == null ? -1 : propertiesButtonItem.Index + 1);
            CommandBar fileCmdBar = ((CommandBars)Connect.ApplicationObject.CommandBars)[commandBarName];
            CommandBarButton fileShortcutCtrl = (CommandBarButton)command.AddControl(fileCmdBar, propertiesStartIndexItem - 1);
            fileShortcutCtrl.Caption = commandCaption;
            fileShortcutCtrl.FaceId = faceId;
            fileShortcutCtrl.BeginGroup = true;   // seperator
        }

        public void Startup(DTE2 applicationObject, AddIn addInInstance)
        {
            
        }

        public vsCommandStatus QueryStatus()
        {
            if (Connect.ApplicationObject == null)
                return vsCommandStatus.vsCommandStatusInvisible;
            Window window = Connect.ApplicationObject.ActiveWindow;
            if (window == null)
                return vsCommandStatus.vsCommandStatusInvisible;

            int count = 0;
            if (window.ObjectKind == EnvDTE.Constants.vsWindowKindSolutionExplorer)
            {
                UIHierarchy hierarchy = Connect.ApplicationObject.ToolWindows.SolutionExplorer;
                count = ((object[])hierarchy.SelectedItems).Length;
            }
            else if (window.ObjectKind == EnvDTE.Constants.vsDocumentKindText)
            {
                count = 1;
            }
            else if (window.Caption.StartsWith("Pending Changes"))
            {
                count = Connect.VersionControlExt.PendingChanges.SelectedItems.Length;
            }

            if (count == 1)
                return vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported;
            else if (count == 0)
                return vsCommandStatus.vsCommandStatusInvisible | vsCommandStatus.vsCommandStatusSupported;
            else
                return vsCommandStatus.vsCommandStatusSupported;
             
        }

        public void Exec()
        {
            Window window = Connect.ApplicationObject.ActiveWindow;
            if (window == null)
                return;

            Workspace workspace = Connect.VersionControlExt.Explorer.Workspace;
            if (workspace == null)
                return;

            if (window.ObjectKind == EnvDTE.Constants.vsWindowKindSolutionExplorer)
            {
                UIHierarchy hierarchy = Connect.ApplicationObject.ToolWindows.SolutionExplorer;
                foreach (UIHierarchyItem selectedItem in (object[])hierarchy.SelectedItems)
                {
                    string filePath = TFSPlusCommon.GetFilePath(selectedItem.Object);
                    string serverPath = workspace.GetServerItemForLocalItem(filePath);
                    TFSPlusCommon.NavigateTo(serverPath);
                }
            }
            else if (window.ObjectKind == EnvDTE.Constants.vsDocumentKindText)
            {
                if (window.Document != null)
                {
                    string serverPath = workspace.TryGetServerItemForLocalItem(window.Document.FullName);
                    if (!String.IsNullOrEmpty(serverPath))
                        TFSPlusCommon.NavigateTo(serverPath);
                }
            }
            else if (window.Caption.StartsWith("Pending Changes"))
            {
                foreach (PendingChange pc in Connect.VersionControlExt.PendingChanges.SelectedItems)
                {
                    TFSPlusCommon.NavigateTo(pc.ServerItem);
                    return;
                }
            }
        }


        public void Disconnect(DTE2 applicationObject, AddIn addInInstance)
        {
        }

        #endregion
    }
}
